﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ppt = Microsoft.Office.Interop.PowerPoint;

namespace ObjectModel.PowerPoint
{
    /// <summary>
    /// Links with a PowerPoint presentation to provide dictionaries for all slides and shapes in
    /// the presentation and provides additional events for that presentation.
    /// </summary>
    public static class PresentationManager
    {
        private static Dictionary<string, Ppt.Shape> mShapes = new Dictionary<string, Ppt.Shape>();
        private static Dictionary<int, Ppt.Slide> mSlides = new Dictionary<int, Ppt.Slide>();
        private static Ppt.Presentation mPres;

        public static PresentationEvents Events { get; private set; }

        public static Dictionary<string, Ppt.Shape> Shapes { get { return mShapes; } }

        public static Dictionary<int, Ppt.Slide> Slides { get { return mSlides; } }

        public static Ppt.Presentation Presentation
        {
            get { return mPres; }
            set
            {
                mPres = value;

                Slides.Clear();
                Shapes.Clear();
                foreach (Ppt.Slide sl in mPres.Slides)
                {
                    Slides.Add(sl.SlideID, sl);

                    foreach (Ppt.Shape sh in sl.Shapes)
                        Shapes.Add(sh.GetUniqueID(sl), sh);
                }

                //Events = new PresentationEvents(value);
                //Events.ShapeAdded += Events_ShapeAdded;
                //Events.ShapeDeleted += Events_ShapeDeleted;
                //Events.SlideAdded += Events_SlideAdded;
                //Events.SlideDeleted += Events_SlideDeleted;
            }
        }

        static void Events_ShapeAdded(object sender, ShapeChangedEventArgs e)
        {
            Shapes.Add(e.ShapeInfo.UniqueId, e.ShapeInfo.Shape);
        }

        static void Events_ShapeDeleted(object sender, ShapeChangedEventArgs e)
        {
            Shapes.Remove(e.ShapeInfo.UniqueId);
        }

        static void Events_SlideAdded(object sender, SlideChangedEventArgs e)
        {
            Slides.Add(e.SlideInfo.Id, e.SlideInfo.Slide);
        }

        static void Events_SlideDeleted(object sender, SlideChangedEventArgs e)
        {
            Slides.Remove(e.SlideInfo.Id);
        }
    }
}
